<?php

namespace MoChat\App\Call\Command;

use Hyperf\Command\Command as HyperfCommand;
use Hyperf\DbConnection\Db;
use Hyperf\Filesystem\FilesystemFactory;
use Hyperf\Redis\Redis;
use Hyperf\Utils\ApplicationContext;
use MoChat\App\Call\Model\CallPlan;
use MoChat\App\Call\Model\CallPlanWorkContact;
use MoChat\App\Sales\Contract\CallRecordContract;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Input\InputArgument;
use TheSeer\Tokenizer\Exception;
use Hyperf\Command\Annotation\Command;
use Hyperf\Di\Annotation\Inject;


/**
 * 处理通话记录
 * @Command
 */
class CallRecordCommand extends HyperfCommand
{

    /**
     * @Inject
     * @var FilesystemFactory
     */
    protected FilesystemFactory $factory;

    /**
     * @var ContainerInterface
     */
    protected $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;

        parent::__construct('callRecord');
    }

    public function configure()
    {
        parent::configure();
        $this->addOption('clear', 'C', InputArgument::OPTIONAL, '清除redis', 0);
        $this->setDescription('遗漏任务');
    }

    /**
     * 生成名称
     * @return string
     */
    private static function generateName()
    {
        return md5(time() . uniqid(rand(), true));
    }

    public function handle()
    {
        $container = ApplicationContext::getContainer();
        $redis = $container->get(Redis::class);
        $clear = $this->input->getOption('clear');
        if ($clear) {
            $redis->del('CallRecordCommand_key');
        }
        $bool = $redis->get('CallRecordCommand_key');
        if ($bool) {
            $this->line('上一个通话记录同步脚本程序正在执行中', 'info');
        } else {
            $redis->set('CallRecordCommand_key', true,60);
            Db::beginTransaction();
            $record = Db::table('call_record')->where('type', 2)->select([
                'id',
                'record_url',
            ])->first();
            try {
                ini_set('allow_url_fopen','on');
               ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;)');
                if (!empty($record)) {
                    $local = $this->factory->get('cos');
                    $pathName = 'phone/' . self::generateName() . '.mp3';
                    $local->write($pathName, file_get_contents($record->record_url));
                    Db::table('call_record')->where('id', $record->id)->update([
                        'type' => 3,
                        'record_url' => env('OSS_HOST') . '/' . $pathName,
                    ]);
                }

                Db::commit();
            } catch (\Throwable $throwable) {
                dump($throwable->getMessage());
                Db::rollBack();
                Db::table('call_record')->where('id', $record->id)->update([
                    'type' => 4,
                ]);
                $this->line('本次通话记录同步失败', 'error');
            }
            $redis->del('CallRecordCommand_key');
            $this->line('本次通话记录同步完成', 'info');
        }
    }
}
